package com.shanpin.shebao.admin.module.news;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shanpin.core.constant.MsgConst;
import com.shanpin.core.thread.ThreadPool;
import com.shanpin.core.utils.DateUtil;
import com.shanpin.shebao.admin.module.city.CityCacheService;
import com.shanpin.shebao.admin.module.sms.SMSService;
import com.shanpin.shebao.model.City;
import com.shanpin.shebao.model.PushMsg;
import com.shanpin.shebao.model.ServiceResult;
import com.shanpin.shebao.utility.PageModel;


@Service
public class NewsService {
	
	@Autowired
	private NewsDao newsDao;
	@Autowired
	private CityCacheService cityCacheService;
	@Autowired
	private SMSService smsService;
	
	public boolean save(NewsForm f) {
		return newsDao.save(f);
	}

	public boolean del(int id) {
		return newsDao.del(id);
	}
	
	public void list(PageModel pageModel) {
		newsDao.list(pageModel);
		List<Map<String, Object>> list = pageModel.getRecordList();
		for (Map<String, Object> map : list) {
			if (null != map.get("send_time")) {
				map.put("send_time", DateUtil.dateStr((Date)map.get("send_time")));
			}
			
			String citys = map.get("citys").toString();
			map.put("citys", "");
			String[] arr = citys.split(",");
			for(String s : arr) {
				City cc = cityCacheService.get(Integer.parseInt(s));
				map.put("citys", map.get("citys").toString() + cc.getCity_name()+ " ");
			}
		}
	}
	
	
	public ServiceResult<String> typeMsg(final TypeMsg msg, String opt_user) throws Exception {
		ServiceResult<Integer> sr = null;
		String regets = ",|，|\\s+";
		if (MsgConst.ALL.equals(msg.getReceive())) {
			sr = newsDao.all(msg, opt_user);
		} else if (MsgConst.TIME.equals(msg.getReceive())) {
			sr = newsDao.time(msg, opt_user);
		} else if (MsgConst.MONTH_ORDER.equals(msg.getReceive())) {
			sr = newsDao.monthOrder(msg, opt_user);
		} else if (MsgConst.PHONE.equals(msg.getReceive())) {
			final String[] phones = msg.getPhones().replace(" ","").split(regets);
			sr = newsDao.phones(msg, phones, opt_user);

			if (msg.isSmsNow()) {
				ThreadPool.submit(new Runnable() {
					
					@Override
					public void run() {
						try {
							smsService.sends(phones, msg.getContent());
						} catch (Exception e) {
						}
					}
				});
			}
			
		}
		if(sr != null) {
			Integer id = (sr.result == null ? 0 : sr.result);
			return ServiceResult.valueOf(id+"");
		}
		return ServiceResult.valueOf("本次没做处理。");
	}
	
	/** 计算用户数量 */
	public int typeMsgCal(TypeMsg msg) {
		int count = newsDao.typeMsgCal(msg);
		return count;
	}
	
	public List<Map<String, Object>> typeMsgList() {
		List<Map<String, Object>> list = newsDao.typeMsgList();
		long mils = 0;
		for (Map<String, Object> map : list) {
			mils = Long.parseLong(map.get("gen_sec").toString()) * 1000;
			map.put("gen_sec", DateTime.now().withMillis(mils).toString("yyyy-MM-dd HH:mm"));
		}
		return list;
	}
	
	public void msgList(PageModel pageModel, String sended) {
		newsDao.msgList(pageModel, sended);
	}
	
	public List<PushMsg> typeMsgDetail(String tmId, String type, String sended) {
		List<PushMsg> list = newsDao.typeMsgDetail(tmId, type, sended);
//		for (Map<String, Object> map : list) {
//			
//		}
		return list;
	}
	public void delTypeMsg(int id) {
		newsDao.delTypeMsg(id);
	}

	public boolean resend(int id) {
		return newsDao.resend(id);
	}
}
